package com.sx.pipeline.ui.login

import android.app.Application
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.bus.RxBus
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.R
import com.sx.pipeline.app.config.LoginType
import com.sx.pipeline.app.event.QQLoginEvent
import com.sx.pipeline.app.event.WxEntryRespEvent
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.repos.http.req.LoginDTO
import com.sx.pipeline.repos.http.res.LoginVO
import com.tencent.mm.opensdk.constants.ConstantsAPI
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers

/**
 * @Author: YY
 * @Date: 2022/5/16
 * @Description: TODO
 */
class LoginPageVM(app: Application): CYBaseViewModel(app) {

    override fun registerRxBus() {
        addSubscribe(
            RxBus.getDefault().toObservable(WxEntryRespEvent::class.java)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { event ->
                    if (event.type == ConstantsAPI.COMMAND_SENDAUTH) {
                        LogUtils.d("微信登录 ${event.extra}")
                        login(LoginType.WECHAT_LOGIN.code,  event.extra)
                    }
                }
        )

        addSubscribe(
            RxBus.getDefault().toObservable(QQLoginEvent::class.java)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe {
                    login(LoginType.QQ_LOGIN.code, it.openid)
                }
        )
    }

    /**
     * 获取测量结果 Tab标题
     */
    fun getTabsList(): List<String> {
        val tabs = mutableListOf<String>()
        tabs.add(getApplication<Application>().getString(R.string.txt_verify_code_login))
        tabs.add(getApplication<Application>().getString(R.string.txt_account_login))
        return tabs
    }

    /**
     * 登录
     */
    fun login(loginType: Int, appCode: String, isBind: Boolean = false) {

        val dto = LoginDTO(loginType.toString(), "",
            "", "", appCode, isBind)

        addSubscribe(mModel.login(dto)
            .compose(RxThreadHelper.rxSchedulerHelper())
            .subscribeWith(object : CommonObserver<LoginVO, BaseViewModel<*>>
                (this, false) {

                override fun onResult(result: LoginVO) {
                    if (result.isBind) {
                        mModel.setToken(result.token!!)
                        // 获取用户信息
                        getUserInfo()
                    } else {
                        // 绑定界面
                        RouteManager.Login.intentToBindPhone(result.openId!!, loginType)
                    }
                }

                override fun onFailed(code: Int, msg: String?) {
                    super.onFailed(code, msg)
                    ToastUtils.showShort("$msg")
                }
            })
        )
    }
}